package com.ymatou.iapi.godmch.testcase;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.condition;
import com.ymatou.iapi.godmch.parameter.deleteConditionMarkupConfigBean;
import com.ymatou.iapi.godmch.parameter.setConditionMarkupConfigBean;
import com.ymatou.iapi.godmch.service.deleteConditionMarkupConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmch.markup_config_condition;
import com.ymttest.database.model.godmch.markup_config_conditionExample;
import com.ymttest.database.model.godmch.markup_config_condition;
import com.ymttest.database.model.godmch.markup_config_conditionExample;
import com.ymttest.database.sqlwapper.godmch.markup_config_conditionWapper;
import com.ymttest.database.sqlwapper.godmch.markup_config_conditionWapper;
/**
*删除按条件自动加价配置
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_deleteConditionMarkupConfig{
	private static deleteConditionMarkupConfigBean bean;
	private static deleteConditionMarkupConfigCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("删除按条件自动加价配置");
	}

	@Before
	public void caseUp() {
		bean=new deleteConditionMarkupConfigBean();
		call=new deleteConditionMarkupConfigCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private markup_config_condition selectConfig(Integer agentId,Integer supplierId) throws InterruptedException
	{
		markup_config_conditionWapper markupConfig=new markup_config_conditionWapper();
		markup_config_conditionExample example=new markup_config_conditionExample();
		example.createCriteria().andAgent_idEqualTo(agentId).andSupplier_idEqualTo(supplierId);
		example.setOrderByClause(" create_time desc");
		markup_config_condition config=markupConfig.selectByExample(example).get(0);
		return config;
	}
	
	private void updateTimeConfig(Integer agentId,Integer supplierId)throws InterruptedException
	{
		   //修改update_time 因为相同的agentid+suppelierId 1分钟内无法修改
		   markup_config_conditionWapper markupConfig=new markup_config_conditionWapper();
		   markup_config_conditionExample example=new markup_config_conditionExample();
			example.createCriteria().andAgent_idEqualTo(agentId).andSupplier_idEqualTo(supplierId);
		   markup_config_condition config=markupConfig.selectByExample(example).get(0);
		   config.setUpdate_time(Date.from((LocalDateTime.now().plusDays(-1).atZone(ZoneId.systemDefault()).toInstant())));
		   markupConfig.updateByPrimaryKey(config);
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_deleteConditionMarkupConfig_001() {
		Logger.start(true,"删除按条件自动加价配置-正常流程-返回成功");
		try {
			
		   Integer agentId=GodmchManageCallService.createAgent();
           Integer supplierId=GodmchManageCallService.createApplySupplier().getSupplierId();
			//新增
           setConditionMarkupConfigBean setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
           GodmchManageCallService.setConditionMarkupConfigCall(setBean);
   		   markup_config_condition config=selectConfig(agentId,supplierId);
   		   
   		   updateTimeConfig(agentId,supplierId);
   		
   		   bean.setAgentId(agentId);
   		   bean.setSupplierId(supplierId);
   		   bean.setMarkupId(config.getMarkup_id());
   		   
		   call.setData(bean);
		   call.callService();			
		   //验证
		   config=selectConfig(agentId,supplierId);
		   Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		   Logger.verifyEquals(-1, config.getConfig_status(), "验证Config_status");
		   
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_deleteConditionMarkupConfig_002() {
		Logger.start(true,"删除按条件自动加价配置-重复删除-返回失败");
		try {
			   Integer agentId=GodmchManageCallService.createAgent();
	           Integer supplierId=GodmchManageCallService.createApplySupplier().getSupplierId();
				//新增
	           setConditionMarkupConfigBean setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	           GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	   		   markup_config_condition config=selectConfig(agentId,supplierId);
	   		   
	   		   updateTimeConfig(agentId,supplierId);
	   		   
	   		   bean.setAgentId(agentId);
	   		   bean.setSupplierId(supplierId);
	   		   bean.setMarkupId(config.getMarkup_id());

			   call.setData(bean);
			   call.callService();	
	   		   updateTimeConfig(agentId,supplierId);
			   call.callService();
			   //验证
			   config=selectConfig(agentId,supplierId);
			   Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			   Logger.verifyEquals(-1, config.getConfig_status(), "验证Config_status");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_deleteConditionMarkupConfig_003() {
		Logger.start(true,"删除按条件自动加价配置-代理商id为空-返回失败");
		try {
			 Integer agentId=GodmchManageCallService.createAgent();
	           Integer supplierId=GodmchManageCallService.createApplySupplier().getSupplierId();
				//新增
	           setConditionMarkupConfigBean setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	           GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	   		   markup_config_condition config=selectConfig(agentId,supplierId);
	   		  // bean.setAgentId(agentId);
	   		   bean.setSupplierId(supplierId);
	   		   bean.setMarkupId(config.getMarkup_id());
	   		   
			   call.setData(bean);
			   call.callService();			
			   //验证
			   Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_deleteConditionMarkupConfig_004() {
		Logger.start(true,"删除按条件自动加价配置-供货商id为空-返回失败");
		try {
			 Integer agentId=GodmchManageCallService.createAgent();
	           Integer supplierId=GodmchManageCallService.createApplySupplier().getSupplierId();
				//新增
	           setConditionMarkupConfigBean setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	           GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	   		   markup_config_condition config=selectConfig(agentId,supplierId);
	   		   bean.setAgentId(agentId);
	   		  // bean.setSupplierId(supplierId);
	   		   bean.setMarkupId(config.getMarkup_id());
	   		   
			   call.setData(bean);
			   call.callService();			
			   //验证
			   Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_deleteConditionMarkupConfig_005() {
		Logger.start(true,"删除按条件自动加价配置-加价配置id为空-返回失败");
		try {
			 Integer agentId=GodmchManageCallService.createAgent();
	           Integer supplierId=GodmchManageCallService.createApplySupplier().getSupplierId();
				//新增
	           setConditionMarkupConfigBean setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	           GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	   		   markup_config_condition config=selectConfig(agentId,supplierId);
	   		   bean.setAgentId(agentId);
	   		   bean.setSupplierId(supplierId);
	   		   //bean.setMarkupId(config.getMarkup_id());
	   		   
			   call.setData(bean);
			   call.callService();			
			   //验证
			   Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_deleteConditionMarkupConfig_006() {
		Logger.start(true,"删除按条件自动加价配置-加价配置id不存在-返回失败");
		try {
			 Integer agentId=GodmchManageCallService.createAgent();
	           Integer supplierId=GodmchManageCallService.createApplySupplier().getSupplierId();
				//新增
	           setConditionMarkupConfigBean setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	           GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	   		  // markup_config_condition config=selectConfig(agentId,supplierId);
	   		   bean.setAgentId(agentId);
	   		   bean.setSupplierId(supplierId);
	   		   bean.setMarkupId(99999);
	   		    
			   call.setData(bean);
			   call.callService();			
			   //验证
			   Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}